package com.dcloud.manage.dbsource.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dcloud.core.dsplugin.IDsService;
import com.dcloud.manage.request.CollectSourceAddRequest;
import com.dcloud.manage.request.CollectSourceRequest;
import com.dcloud.manage.service.entity.CollectSource;
import com.dcloud.manage.service.ICollectSourceService;
import com.dcloud.manage.vo.CollectSourceVO;
import com.dcloud.manage.vo.SourceTestConnVO;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 数据源表
 * @Author: jeecg-boot
 * @Date:   2022-09-07`
 * @Version: V1.0
 */
@Api(tags="数据源表")
@RestController
@RequestMapping("/api/collectSource/v1")
@Slf4j
public class CollectSourceController extends JeecgController<CollectSource, ICollectSourceService> {
	@Autowired
	private ICollectSourceService collectSourceService;

	@Autowired
	 IDsService iDsService;
	
	/**
	 * 分页列表查询
	 *

	 * @return
	 */
	@AutoLog(value = "数据源表-列表查询")
	@ApiOperation(value="数据源表-列表查询", notes="数据源表-列表查询")
	@PostMapping(value = "/list")
	public Result<?> queryList(@RequestBody CollectSourceRequest collectSourceRequest) {

		List<CollectSourceVO> list = collectSourceService.queryListByReq(collectSourceRequest);
		return Result.OK(list);
	}


	 /**
	  * 根据数据源id获取数据源详情
	  *

	  * @return
	  */
	 @AutoLog(value = "数据源表-列表查询")
	 @ApiOperation(value="数据源表-列表查询", notes="数据源表-列表查询")
	 @GetMapping(value = "/info/byid")
	 public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		 CollectSourceVO list = collectSourceService.queryInfoById(id);
		 return Result.OK(list);
	 }


	 /**
	  * 分页列表查询
	  *
	  * @param collectSourceRequest
	  * @return
	  */
	 @AutoLog(value = "数据源表-分页列表查询")
	 @ApiOperation(value="数据源表-分页列表查询", notes="数据源表-分页列表查询")
	 @PostMapping(value = "/page")
	 public Result<?> queryPageListNew(@RequestBody CollectSourceRequest collectSourceRequest) {
		 IPage<CollectSourceVO> collectSourceIPage = collectSourceService.queryByPage(collectSourceRequest);
		 return Result.OK(collectSourceIPage);
	 }
	
	/**
	 *  编辑
	 *
	 * @param collectSource
	 * @return
	 */
	@AutoLog(value = "数据源表-编辑")
	@ApiOperation(value="数据源表-编辑", notes="数据源表-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody CollectSource collectSource) {
		collectSourceService.updateById(collectSource);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "数据源表-通过id删除")
	@ApiOperation(value="数据源表-通过id删除", notes="数据源表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		collectSourceService.deleteById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "数据源表-批量删除")
	@ApiOperation(value="数据源表-批量删除", notes="数据源表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.collectSourceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	

	 /**
	  *   新增数据源配置
	  *
	  * @param collectSourceAddRequest
	  * @return
	  */
	 @AutoLog(value = "数据源表-添加")
	 @ApiOperation(value="数据源表-添加", notes="数据源表-添加")
	 @PostMapping(value = "/add")
	 public Result<?> addNew(@RequestBody CollectSourceAddRequest collectSourceAddRequest) {
		 collectSourceService.saveNew(collectSourceAddRequest);
		 return Result.OK("添加成功！");
	 }

	 /**
	  *   新增数据源配置
	  *
	  * @return
	  */
	 @AutoLog(value = "数据源表-查询")
	 @ApiOperation(value="数据源表-添加", notes="数据源表-查询")
	 @PostMapping(value = "/getAllSourse")
	 public Result<?> list() {
		 List<CollectSource> list = collectSourceService.list(new LambdaQueryWrapper<>());
		 return Result.OK(list);
	 }

	 /**
	  * @Author shijf
	  * @Description //测试数据源是否可连接
	  * @Date 14:45 2023/5/24
	  * @return org.jeecg.common.api.vo.Result<?>
	  **/
	 @AutoLog(value = "数据源表-测试连接")
	 @ApiOperation(value="数据源表-添加", notes="数据源表-添加")
	 @PostMapping(value = "/testConn")
	 public Result<?> list(@RequestParam(name="tid",required=true) Integer tid) {

		 try {
			 return iDsService.testConn(tid);
		 } catch (Exception e ) {
			 log.error(e.getMessage());
			 return Result.error("未知错误失败,{}",e.getMessage());
		 }

	 }

	 /**
	  * @Author shijf
	  * @Description //获取数据源所有的表
	  * @Date 14:45 2023/5/24
	  * @return org.jeecg.common.api.vo.Result<?>
	  **/
	 @AutoLog(value = "数据源表-测试连接")
	 @ApiOperation(value="数据源表-添加", notes="数据源表-添加")
	 @GetMapping(value = "/getAllTableFromDs")
	 public Result<?> getAllTableFromDs(@RequestParam(name="tid",required=true) Integer tid) {

		 try {
			 return iDsService.getAllTableFromDs(tid);
		 } catch (Exception e ) {
			 return Result.error("未知错误失败,{}",e.getMessage());
		 }

	 }

	 /**
	  * @Author shijf
	  * @Description //根据数据源类型，查询相对应的数据源
	  * @Date 14:45 2023/5/24
	  * @return org.jeecg.common.api.vo.Result<?>
	  **/
	 @AutoLog(value = "数据源表-根据数据源类型，查询相对应的数据源")
	 @ApiOperation(value="数据源表-根据数据源类型，查询相对应的数据源", notes="数据源表-根据数据源类型，查询相对应的数据源")
	 @GetMapping(value = "/getAllSourceByType")
	 public Result<?> getAllSourceByType(@RequestParam(name="sourceCode",required=true) String sourceCode) {

		 try {
			 return iDsService.getAllSourceByType(sourceCode);
		 } catch (Exception e ) {
			 return Result.error("未知错误失败,{}",e.getMessage());
		 }

	 }
}
